﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;
using SistemaInventarioMVC.Models.Entidades;
using System.Data.Entity.ModelConfiguration.Conventions;

namespace SistemaInventarioMVC.Models.Context
{
    public class SistemaInventarioDbContext : DbContext
    {
        private static SistemaInventarioDbContext _instance;

        //Tablas
        public DbSet<Almacen> Almacenes { get; set; }
        public DbSet<Articulo> Articulos { get; set; }
        public DbSet<CabeceraEntradaAlmacen> CabecerasEntradaAlmacen { get; set; }
        public DbSet<CabeceraSalidaAlmacen> CabecerasSalidaAlmacen { get; set; }
        public DbSet<DetalleEntradaAlmacen> DetallesEntradaAlmacen { get; set; }
        public DbSet<DetalleSalidaAlmancen> DetallesSalidaAlmacen { get; set; }
        public DbSet<Familia> Familias { get; set; }
        public DbSet<Inventario> Inventarios { get; set; }
        public DbSet<Marca> Marcas { get; set; }
        public DbSet<RazonSalida> RazonesSalida { get; set; }
        public DbSet<RazonEntrada> RazonesEntrada { get; set; }
        public DbSet<TipoProducto> TiposProducto { get; set; }
        public DbSet<Ubicacion> Ubicaciones { get; set; }
        public DbSet<UnidadMedida> UnidadesMedida { get; set; }

        //Constructor
        public SistemaInventarioDbContext() 
        {
            this.Database.CreateIfNotExists();
            this.Configuration.LazyLoadingEnabled = true;
            this.Configuration.ValidateOnSaveEnabled = false;
        }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);
            modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
        }

        public static SistemaInventarioDbContext Instance
        {
            get
            {
                if (_instance == null)
                {
                    _instance = new SistemaInventarioDbContext();
                }
                return _instance;
            }
        }

        public static SistemaInventarioDbContext Current
        {
            get
            {  
                
                var db = HttpContext.Current.Items["__DbContext"] as SistemaInventarioDbContext;
                if (db == null)
                {
                   db = new SistemaInventarioDbContext();
                    HttpContext.Current.Items["__DbContext"] = db;
                }
                return db;
            }
        }
    }
}